package com.aispeech.astools.service;

import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;

import androidx.annotation.Nullable;


/**
 * @author rain  06.21 2019
 */

public class FloatingService extends Service {

    private final static String TAG = "FloatingService";
    private LayoutParams mLayoutParams;
    private WindowManager mWindowManager;
    private Button mFloatButton;
    private boolean isShow = false;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand with: intent = " + intent + ", flags = " + flags + ", startId = " + startId + "");
        showFloatingWindow();
        return super.onStartCommand(intent, flags, startId);
    }

    private void showFloatingWindow() {
        if (VERSION.SDK_INT >= VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                Log.w(TAG, "can not DrawOverlays");
                return;
            }
        }

        if (isShow) {
            return;
        }

        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        mFloatButton = new Button(getApplicationContext());
        mFloatButton.setText("Floating Window Test");
        mFloatButton.setBackgroundColor(Color.GRAY);
        mFloatButton.setOnTouchListener(new FloatingOnTouchListener());
        mFloatButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mWindowManager.removeView(mFloatButton);
                isShow = false;
            }
        });

        // 设置LayoutParam
        mLayoutParams = new LayoutParams();
        if (VERSION.SDK_INT >= VERSION_CODES.O) {
            mLayoutParams.type = LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            mLayoutParams.type = LayoutParams.TYPE_PHONE;
        }

        mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            | WindowManager.LayoutParams.FLAG_SCALED
            | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
            | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

        mLayoutParams.format = PixelFormat.RGBA_8888;
        mLayoutParams.width = 500;
        mLayoutParams.height = 200;
//        mLayoutParams.x = 300;
//        mLayoutParams.y = 300;
        mLayoutParams.alpha = 0.8f;
        mLayoutParams.gravity = Gravity.CENTER;

        assert mWindowManager != null : "windowManager is null";
        mWindowManager.addView(mFloatButton, mLayoutParams);
        isShow = true;
    }

    private class FloatingOnTouchListener implements View.OnTouchListener {

        private int x;
        private int y;

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x = (int) event.getRawX();
                    y = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int nowX = (int) event.getRawX();
                    int nowY = (int) event.getRawY();
                    int movedX = nowX - x;
                    int movedY = nowY - y;
                    x = nowX;
                    y = nowY;
                    mLayoutParams.x = mLayoutParams.x + movedX;
                    mLayoutParams.y = mLayoutParams.y + movedY;

                    // 更新悬浮窗控件布局
                    mWindowManager.updateViewLayout(view, mLayoutParams);
                    break;
                default:
                    break;
            }
            return false;
        }
    }
}
